package com.zy.mc_module;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.zy.mc_module.base.kotlin.BaseFragment;
import com.zy.mc_module.common.Constant;
import com.zy.mc_module.databinding.FragmentMineBinding;
import com.zy.mc_module.mvp.base.BasePresenter;
import com.zy.mc_module.mvp.base.IView;
import com.zy.mc_module.mvp.bean.BaseBean;
import com.zy.mc_module.mvp.bean.UserBean;
import com.zy.mc_module.ui.login.LoginActivity;
import com.zy.mc_module.ui.micang.setting.MiCangSettingActivity;
import com.zy.mc_module.utils.GsonUtil;
import com.zy.mc_module.utils.SPUtil;
import com.zy.mc_module.utils.StringsUtils;
import com.zy.mc_module.utils.ToastUtil;
import com.zy.mc_module.utils.retrofit.RetrofitManager;
import com.zy.mc_module.utils.retrofit.RxObserver;
import com.zy.mc_module.utils.retrofit.RxTransform;
import com.zy.mc_module.utils.retrofit.error.ThdException;

public class MineFragment extends BaseFragment<IView, BasePresenter<IView>, FragmentMineBinding> {

    @NonNull
    @Override
    public FragmentMineBinding getViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        return FragmentMineBinding.inflate(inflater, container, false);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (StringsUtils.isEmpty(SPUtil.getString(Constant.USER_TOKEN_INFO,""))){
            binding.goLogin.setVisibility(View.VISIBLE);
            binding.loginYes.setVisibility(View.GONE);
        }else {
            binding.goLogin.setVisibility(View.GONE);
            binding.loginYes.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void LazyLoad() {

        RetrofitManager.getInstance().getApi().getUserInfo().compose(RxTransform.schedule())
                .subscribe(new RxObserver<UserBean>(null,getContext()) {
                    @Override
                    protected void onBaseNext(UserBean userBean) {
                        ToastUtil.show(GsonUtil.gsonString(userBean));
                    }

                    @Override
                    protected void onBaseError(ThdException e) {
                        ToastUtil.show(e.message);
                    }
                });


    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        binding.goLogin.setOnClickListener(v -> startActivity(new Intent(mContext, LoginActivity.class)));
        binding.mingSetting.setOnClickListener(v -> startActivity(new Intent(mContext, MiCangSettingActivity.class)));
    }
}
